package com.um.jdy.common.shop.service.impl;

import cn.hutool.core.util.StrUtil;
import com.um.jdy.common.market.dao.ActivityGoodsOptionDao;
import com.um.jdy.common.market.entity.po.ActivityGoodsOption;
import com.um.jdy.common.shop.code.ShopGoodsCode;
import com.um.jdy.common.shop.code.ShopOrderCode;
import com.um.springboot.starter.config.SystemSQL;
import com.um.springboot.starter.dao.CommonDao;
import com.um.springboot.starter.exception.ServiceException;
import com.um.springboot.starter.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import com.um.springboot.starter.orm.service.BaseEntityServiceImpl;
import com.um.jdy.common.shop.service.GoodsOptionService;
import com.um.jdy.common.shop.dao.GoodsOptionDao;
import com.um.jdy.common.shop.entity.po.GoodsOption;

import java.util.List;
import java.util.Map;

/**
 * 服务实现类：规格组合表
 */
@Service
@Transactional
@Slf4j
public class GoodsOptionServiceImpl extends BaseEntityServiceImpl<GoodsOptionDao,GoodsOption>  implements GoodsOptionService{

    @Autowired
    CommonDao commonDao;

    @Autowired
    ActivityGoodsOptionDao activityGoodsOptionDao;

    /**
     * 增加库存
     *
     * @param mallGoodsOptionId
     * @param count
     * @return
     */
    @Override
    public boolean addStock(String mallGoodsOptionId, int count) {
        GoodsOption option = getRow(mallGoodsOptionId);

        if(null != option){

            int stock = option.getStock();

            String sql = SystemSQL.getInstance().getSystemSQL("CAS_update_goods_option_stock");
            boolean result = commonDao.executeSQL(sql,new Object[]{stock+count,mallGoodsOptionId,stock});

            if(!result){
                return this.addStock(mallGoodsOptionId,count);
            }
            return result;
        }

        return false;
    }

    /**
     * 减少库存 （乐观锁的方式）
     * @param mallGoodsOptionId
     * @param count
     * @return
     */
    @Override
    public boolean reduceStock(String mallGoodsOptionId, int count) {
        GoodsOption option = getRow(mallGoodsOptionId);
        if(null == option){
            throw new ServiceException(ShopGoodsCode.GoodsOptionNotExist);
        }

        int stock = option.getStock();
        if(!(count > 0 && stock >= count)){
            throw new ServiceException(ShopOrderCode.StockLack);
        }

        String sql = SystemSQL.getInstance().getSystemSQL("CAS_update_goods_option_stock");
        boolean result = commonDao.executeSQL(sql,new Object[]{stock-count,mallGoodsOptionId,stock});

        if(!result){
            return this.reduceStock(mallGoodsOptionId,count);
        }
        return result;
    }

    /**
     * 减少库存 （乐观锁的方式）
     *
     * @param marketActivityGoodsOptionId
     * @param count
     * @return
     */
    @Override
    public boolean reduceActivityStock(String marketActivityGoodsOptionId, int count) {
        if(StrUtil.isEmpty(marketActivityGoodsOptionId)){
            return false;
        }

        ActivityGoodsOption option = activityGoodsOptionDao.getRow(marketActivityGoodsOptionId);
        if(null == option){
            throw new ServiceException(ShopGoodsCode.GoodsOptionNotExist);
        }

        int stock = option.getActivityStock();
        if(!(count > 0 && stock >= count)){
            throw new ServiceException(ShopOrderCode.StockLack);
        }


        String sql = SystemSQL.getInstance().getSystemSQL("CAS_update_goods_option_activity_stock");
        boolean result = commonDao.executeSQL(sql,new Object[]{stock-count,marketActivityGoodsOptionId,stock});

        if(!result){
            return this.reduceStock(marketActivityGoodsOptionId,count);
        }
        return result;
    }

    /**
     * 增加库存
     *
     * @param marketActivityGoodsOptionId
     * @param count
     * @return
     */
    @Override
    public boolean addActivityStock(String marketActivityGoodsOptionId, int count) {
        if(StrUtil.isEmpty(marketActivityGoodsOptionId)){
            return false;
        }
        ActivityGoodsOption option = activityGoodsOptionDao.getRow(marketActivityGoodsOptionId);

        if(null != option){

            int stock = option.getActivityStock();

            String sql = SystemSQL.getInstance().getSystemSQL("CAS_update_goods_option_activity_stock");
            boolean result = commonDao.executeSQL(sql,new Object[]{stock+count,marketActivityGoodsOptionId,stock});

            if(!result){
                return this.addStock(marketActivityGoodsOptionId,count);
            }
            return result;
        }

        return false;
    }
}
